package com.lihao.jetpackcore.network.manager

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.net.ConnectivityManager
import android.widget.Toast
import com.lihao.jetpackcore.util.NetworkUtil

/**
 * Created by lihao on 2020/5/14.
 * Describe:
 */
class NetworkStateReceive : BroadcastReceiver() {

    override fun onReceive(context: Context?, intent: Intent) {
        if (intent.action == ConnectivityManager.CONNECTIVITY_ACTION) {
            if (!NetworkUtil.isNetworkAvailable(context)) {
                //收到没有网络时判断之前的值是不是有网络，如果有网络才提示通知 ，防止重复通知
                NetworkStateManager.instance.mNetworkStateCallback.value?.let {
                    if (it.isSuccess) {
                        Toast.makeText(context, "网络不给力啊", Toast.LENGTH_SHORT).show()
                        NetworkStateManager.instance.mNetworkStateCallback.postValue(
                            NetState(
                                isSuccess = false
                            )
                        )
                    }
                }
            } else {
                //收到有网络时判断之前的值是不是没有网络，如果没有网络才提示通知 ，防止重复通知
                NetworkStateManager.instance.mNetworkStateCallback.value?.let {
                    if (!it.isSuccess) {
                        NetworkStateManager.instance.mNetworkStateCallback.postValue(
                            NetState(
                                isSuccess = true
                            )
                        )
                    }
                }
            }
        }
    }
}